package com.example.administrator.managesystem.adapter;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.example.administrator.managesystem.R;
import com.example.administrator.managesystem.utils.DisplayUtils;
import com.example.administrator.managesystem.view.SlidingButtonView;
import java.util.ArrayList;
import java.util.List;

public class NormalRecyclerViewAdapter extends RecyclerView.Adapter<NormalRecyclerViewAdapter.MyViewHolder> implements SlidingButtonView.IonSlidingButtonListener {
    private Context mContext;

    private IonSlidingViewClickListener mIDeleteBtnClickListener;
    public void addListener(IonSlidingViewClickListener mIDeleteBtnClickListener){
        this.mIDeleteBtnClickListener=mIDeleteBtnClickListener;
    }

    private List<String> mDatas = new ArrayList<String>();

    private SlidingButtonView mMenu = null;

    public NormalRecyclerViewAdapter(Context context) {

        mContext = context;
        for (int i = 0; i < 10; i++) {
            mDatas.add("土豆哪里去挖？土豆郊区去挖。一挖一麻袋？一挖一麻袋.");
        }
    }

    @Override
    public int getItemCount() {
        return mDatas.size();
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, int position) {

        // holder.record.setText(mDatas.get(position));

        //设置内容布局的宽为屏幕宽度
        holder.layout_content.getLayoutParams().width = DisplayUtils.getScreenWidthPixels(mContext);

        holder.layout_content.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //判断是否有删除菜单打开
                Log.e("msg","menuIsOpen():"+menuIsOpen());
                if (menuIsOpen()) {
                    Log.e("msg","menuIsOpen():"+menuIsOpen());
                    closeMenu();//关闭菜单
                } else {
                    int n = holder.getLayoutPosition();
                    mIDeleteBtnClickListener.onItemClick(v, n);
                }

            }
        });
        holder.btn_Delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int n = holder.getLayoutPosition();
                mIDeleteBtnClickListener.onDeleteBtnClick(v, n);
            }
        });
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup arg0, int arg1) {

        View view = LayoutInflater.from(mContext).inflate(R.layout.land_recycler_item, arg0, false);
        MyViewHolder holder = new MyViewHolder(view);

        return holder;
    }

    public void addData(int position) {
        mDatas.add(position, "添加项");
        notifyItemInserted(position);
    }

    public void removeData(int position) {
        mDatas.remove(position);
        notifyItemRemoved(position);

    }

    /**
     * 删除菜单打开信息接收
     */
    @Override
    public void onMenuIsOpen(View view) {
        mMenu = (SlidingButtonView) view;
    }

    /**
     * 滑动或者点击了Item监听
     *
     * @param slidingButtonView
     */
    @Override
    public void onDownOrMove(SlidingButtonView slidingButtonView) {
        if (menuIsOpen()) {
            if (mMenu != slidingButtonView) {
                closeMenu();
            }
        }
    }

    /**
     * 关闭菜单
     */
    public void closeMenu() {
        mMenu.closeMenu();
        mMenu = null;

    }

    /**
     * 判断是否有菜单打开
     */
    public Boolean menuIsOpen() {
        if (mMenu != null) {
            return true;
        }
        return false;
    }

    public interface IonSlidingViewClickListener {
        void onItemClick(View view, int position);

        void onDeleteBtnClick(View view, int position);
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        public TextView btn_Delete;
        private ImageView user_img,product_img;
        public TextView name,time,record;
        public ViewGroup layout_content;

        public MyViewHolder(View itemView) {
            super(itemView);
            btn_Delete =  itemView.findViewById(R.id.tv_delete);
//            name =  itemView.findViewById(R.id.name);
//            time =  itemView.findViewById(R.id.time);
//            record =  itemView.findViewById(R.id.record);
//            user_img=itemView.findViewById(R.id.user_img);
//           product_img=itemView.findViewById(R.id.product_img);
            layout_content =  itemView.findViewById(R.id.layout_content);

            ((SlidingButtonView) itemView).setSlidingButtonListener(NormalRecyclerViewAdapter.this);
        }
    }


}

